package main.demonutils.io;

import main.model.hexisland.HexIsland;
import main.model.hexisland.listener.HexIslandListener;

import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: gabe
 * Date: 02/07/13
 * Time: 20:06
 * To change this template use File | Settings | File Templates.
 */
public class IslandIOController {
    Logger log;


    private HexIsland hexIsland = null;
    //private static final File DEFAULT_DIR = new File("data");
    private File currentDir = null;
    private File currentFile = null;


    private ArrayList<String> selectedExtensions = new ArrayList<String>();

    private boolean isEdited = false;
    private IslandIOStrategyCSV ioStrategy = new IslandIOStrategyCSV();

    private JFileChooser fileChooser;



    public IslandIOController(HexIsland hexIsland){
        log = Logger.getLogger("IO LOG");
        log.setLevel(Level.ALL);
        if( hexIsland == null ) throw new IllegalStateException("Null hexisland at iocontroller start");
        this.hexIsland = hexIsland;
        hexIsland.addIslandListener(new HexIslandListener() {
            @Override
            public void onTileChange(int q, int r) {
                isEdited = true;
            }

            @Override
            public void onIslandChange() {
                currentFile = null;
                isEdited = false;
            }
        });

        selectedExtensions.add(IOUtils.LEGALEXTENSIONS[0]);
    }

    public boolean isEdited(){
        return isEdited;
    }

    public HexIsland getIsland(){
        return this.hexIsland;
    }


    public boolean saveIsland(){
        log.info("Saving - current file is "+currentFile);
        if(currentFile == null) return false;
        if(currentFile.exists()) {
            if(!currentFile.delete()) return false;
        }
        return ioStrategy.writeHexIsland(hexIsland , currentFile);
    }

    public boolean loadIsland(File fileToLoad){
        if(currentFile == null) return false;
        if(ioStrategy.readHexIsland(hexIsland , fileToLoad)){
            currentFile = fileToLoad;
            return true;
        }
        return false;
    }

    public File chooseDirectory(){
        File chosenOption = IOUtils.chooseDirectory(currentDir);
        if(chosenOption != null){
            currentDir = chosenOption;
        }
        log.info("choosing dir - current dir is "+currentDir);
        return currentDir;
    }

    public File prepareNewFileLocation(){
        File chosenOption = IOUtils.prepareNewFileLocation(currentDir , IOUtils.LEGALEXTENSIONS[0]);
        if(chosenOption != null){
            currentFile = chosenOption;
        }
        log.info("preparing space - file is "+currentFile);
        return currentFile;
    }

    public String getCurrentDirPathString(){
        return IOUtils.getCurrentDirPathString(currentDir);
    }

    public String getCurrentFileName(){
        return currentFile.getName();
    }

    public File[] getDirContent(){
       return IOUtils.getDirContent(currentDir, selectedExtensions);
    }

    public boolean hasCurrentFile(){
        return currentFile != null;
    }

    public boolean deleteFile(File f){
        return IOUtils.deleteFile(f);
    }

    public boolean hasCurrentDir() {
        return currentDir != null;
    }
}
